//
//  NetworkTools.m
//  测试AFNetworking
//
//  Created by 伍银荣 on 2021/12/20.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "NetworkTools.h"

//@protocol NetworkToolsProtocol <NSObject>
//
//- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
//                                       URLString:(NSString *)URLString
//                                      parameters:(nullable id)parameters
//                                         headers:(nullable NSDictionary <NSString *, NSString *> *)headers
//                                  uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
//                                downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
//                                         success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
//                                         failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
//
//@end


@implementation NetworkTools

+(instancetype)sharedTools {
    static NetworkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NetworkTools alloc]initWithBaseURL:[NSURL URLWithString:@"http://www.weather.com.cn/"]];
        //设置反序列化数据格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    });
    return  instance;
}


- (void)requestWithMethod:(RequestMethod)method UrlString:(NSString *)URLString parameters:(id)param headers:(NSDictionary <NSString *, NSString *> *)headers progress:(void (^)(NSProgress *downloadProgress))progress finished:(void(^)(id result, NSError *error))finished{
    
    NSString *methodName = (method == GET) ? @"GET" : @"POST";
    
    [[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:param headers:headers uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
        
    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
        progress(downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finished(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finished(nil, error);
    }] resume];
    
    
//    if (method == GET) {
//        [self GET:URLString parameters:param headers:headers progress:^(NSProgress * _Nonnull downloadProgress) {
//            //执行回调
//            progress(downloadProgress);
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            finished(responseObject, nil);
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            finished(nil, error);
//        }];
//    } else {
//        [self POST:URLString parameters:param headers:headers progress:^(NSProgress * _Nonnull downloadProgress) {
//            //执行回调
//            progress(downloadProgress);
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            finished(responseObject, nil);
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            finished(nil, error);
//        }];
//    }
    
    
    
}

@end
